package com.ssm.xm.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.xm.pojo.Student;
import com.ssm.xm.pojo.User;
import com.ssm.xm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/findAll")
    @ResponseBody //声明当前控制器返回JSON格式的数据
    public List<User> getAllUser(){
        List<User> allUser = userService.getAllUser();
        System.out.println("柳艾迪");
        return allUser;
    }

    @RequestMapping("/page")
    public String page(
            @RequestParam(required = false,defaultValue = "1")Integer pageNum,
            @RequestParam(required = false,defaultValue = "3")Integer pageSize,
            Model model
    ){
        PageHelper.startPage(pageNum,pageSize);
        List<User> allUser = userService.getAllUser();
        PageInfo<User> pageInfo = new PageInfo<>(allUser);
        model.addAttribute("pageInfo",pageInfo);
        return "page";
    }

    @RequestMapping("/pageStudent")
    public String pageStudent(
            @RequestParam(required = false,defaultValue = "1")Integer pageNum,
            @RequestParam(required = false,defaultValue = "4")Integer pageSize,
            @RequestParam(required = false,defaultValue = "")String likeName,
            Model model
    ){
        PageHelper.startPage(pageNum,pageSize*2);
        List<Student> allUser = userService.getPageStudent(likeName);
        PageInfo<Student> pageInfo = new PageInfo<>(allUser);
        model.addAttribute("pageInfo",pageInfo);
        return "home";
    }

    @ModelAttribute
    public void method(){
        System.out.println("在任何控制器之前");
    }
}